package com.jiezuo.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiezuo.auth.entity.UserEntrySet;
import com.jiezuo.auth.mapper.UserEntrySetMapper;
import com.jiezuo.auth.service.UserEntrySetService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 庞留杰
 * @version V1.0
 * @description 用户自定义列表明细
 * @date 2023/11/22 16:39
 **/
@Transactional
@Slf4j
@Service
public class UserEntrySetServiceImpl extends ServiceImpl<UserEntrySetMapper, UserEntrySet> implements UserEntrySetService {

    @Override
    public IPage<UserEntrySet> findPage(Page initMpPage, UserEntrySet userEntrySet) {
        QueryWrapper<UserEntrySet> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
        return this.page(initMpPage, queryWrapper);
    }

    @Override
    public List<UserEntrySet> findList(UserEntrySet userEntrySet) {
        QueryWrapper<UserEntrySet> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
        List<UserEntrySet> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public UserEntrySet findById(String id) {
        return this.getById(id);
    }

    @Override
    public boolean add(UserEntrySet userEntrySet) {
        return this.save(userEntrySet);
    }

    @Override
    public boolean update(UserEntrySet userEntrySet) {
        if (Boolean.FALSE.equals(userEntrySet.getVisible())) {
            userEntrySet.setIs_visible("0");
        } else {
            userEntrySet.setIs_visible("1");
        }
        if (Boolean.FALSE.equals(userEntrySet.getShowOverflow())) {
            userEntrySet.setIs_showOverflow("0");
        } else {
            userEntrySet.setIs_showOverflow("1");
        }
        if (Boolean.FALSE.equals(userEntrySet.getSortable())) {
            userEntrySet.setIs_sortable("0");
        } else {
            userEntrySet.setIs_sortable("1");
        }
        return this.updateById(userEntrySet);
    }

    @Override
    public boolean updateBatch(UserEntrySet userEntrySet) {
        userEntrySet.getSetList().forEach(set -> {
            if (Boolean.FALSE.equals(set.getVisible())) {
                set.setIs_visible("0");
            } else {
                set.setIs_visible("1");
            }
            if (Boolean.FALSE.equals(set.getShowOverflow())) {
                set.setIs_showOverflow("0");
            } else {
                set.setIs_showOverflow("1");
            }
            if (Boolean.FALSE.equals(set.getSortable())) {
                set.setIs_sortable("0");
            } else {
                set.setIs_sortable("1");
            }
        });
        return this.updateBatchById(userEntrySet.getSetList());
    }

    @Override
    public boolean delete(String id) {
        UserEntrySet userEntrySet = new UserEntrySet();
        userEntrySet.setId(id);
        userEntrySet.setIs_delete("1");
        return this.updateById(userEntrySet);
    }
}
